package com.lanyou.cook.web;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.InterfaceScheduleResult;
import com.lanyou.cook.repository.InterfaceScheduleResultRepository;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.cook.util.DateUtil;
import com.lanyou.cook.web.param.InterfaceScheduleResultParam;
/**
 * 
 * @author 仇伟业
 *
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/interfaceScheduleResults")
public class InterfaceScheduleResultController {
	@Autowired
	UserSessionService userSessionService;
	@Autowired
	private InterfaceScheduleResultRepository interfaceScheduleResultRepository;
	
	/**
	 * 查询接口调度历史
	 * @param param 类型: InterfaceScheduleResultParam
	 * @param pageable: Pageable
	 * @return 带参数条件的所有作业信息
	 */
	@RequestMapping("/search/findByExample")
	@RequiresPermissions(value={"interfaceSchedulingHistoryQuery:view"})
	public Page<InterfaceScheduleResult> findByExample(InterfaceScheduleResultParam param, Pageable pageable) {
		Specification<InterfaceScheduleResult> spec = new Specification<InterfaceScheduleResult>() {
			@Override
			public Predicate toPredicate(Root<InterfaceScheduleResult> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> predicatesList = new ArrayList<>();
				
				if (StringUtils.isNoneBlank(param.getFieldName())) {
					predicatesList.add(cb.equal(root.get("fieldName"), param.getFieldName()));
				}
				
				if (StringUtils.isNoneBlank(param.getFieldSystemName())) {
					predicatesList.add(cb.equal(root.get("fieldSystemName"), param.getFieldSystemName()));
				}else{
					Path<String> pathOwnerFieldSystemName = root.get("fieldSystemName");
					// 默认查当前登录用户所管理的领域系统
					Collection<String> fieldSystemNames = userSessionService.findFieldSystemNamesOfCurrentAuthenticatedUserCanManage();
					if(!CollectionUtils.isEmpty(fieldSystemNames)){
						predicatesList.add(pathOwnerFieldSystemName.in(fieldSystemNames));
					}else {
						predicatesList.add(cb.notEqual(cb.literal(1), 1));
					}
				}
				
				if (StringUtils.isNotBlank(param.getInterfaceName())) {
					predicatesList.add(cb.like(root.get("interfaceName"), '%' + param.getInterfaceName() + "%"));
				}
				
				if (StringUtils.isNotBlank(param.getScheduleName())) {
					predicatesList.add(cb.like(root.get("scheduleName"), "%" + param.getScheduleName() + "%"));
				}
				
				if (StringUtils.isNotBlank(param.getScheduleResult())) {
					predicatesList.add(cb.equal(root.get("scheduleResult"), param.getScheduleResult()));
				}
				if (param.getBeginOfScheduleTime()==null) {
					param.setBeginOfScheduleTime("1900-1-1 00:00:00");
				}
				
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					Date beginDate = sdf.parse(param.getBeginOfScheduleTime());
					predicatesList.add(cb.greaterThan(root.get("scheduleTime"), beginDate));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if (param.getEndOfScheduleTime()==null) {
					param.setEndOfScheduleTime("2999-01-01 00:00:00");
				}
				
				try {
					Date endDate = sdf.parse(param.getEndOfScheduleTime());
					predicatesList.add(cb.lessThan(root.get("scheduleTime"), endDate));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				query.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
				return null;
			}
		};
 
		return interfaceScheduleResultRepository.findAll(spec, pageable);
	}
}
